package com.qianchang.microfinance.view.hsmywallet;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.FormatUtil;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.BankCardBean;
import com.qianchang.microfinance.bean.FindUserBasicInfoBean;
import com.qianchang.microfinance.bean.MineUserInfo;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseFragment;
import com.qianchang.microfinance.view.dialog.alert.DimengSingleButtonAlertDialog;
import com.qianchang.microfinance.view.home.HomeFragment;
import com.qianchang.microfinance.view.hsbindbank.HSBindBankCardActivity;
import com.qianchang.microfinance.view.hsrecharge.HSReChargeActivity;
import com.qianchang.microfinance.view.hsrecharge.HSReChargeAuthorizedActivity;
import com.qianchang.microfinance.view.mine.RechargeActivity;
import com.qianchang.microfinance.view.mine.TradRecordActivity;
import com.qianchang.microfinance.view.mine.WithDrawActivity;

/**
 * 鸿胜科技新增我的钱包余额充值
 *
 */

public class HSRecharBalanceFragment extends BaseFragment implements View.OnClickListener{

    private Button butRecharge;
    private GridView blanceGrid;
    private HSRecharBalanceAdapter adapter;
    private EditText moneyEdit;
    private int index = -1;
    private HSRecharBalanceAdapter.MoneyBean moneyBean = null;

    @Override
    protected View onRootViewCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_recharbalance, container, false);
        butRecharge = (Button) view.findViewById(R.id.btn_recharge);
        butRecharge.setOnClickListener(this);
        blanceGrid = (GridView) view.findViewById(R.id.gv_blance_id);
        moneyEdit = (EditText) view.findViewById(R.id.et_money_id);
        adapter = new HSRecharBalanceAdapter(getContext());
        blanceGrid.setAdapter(adapter);
        blanceGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                adapter.setIndex(position);
                moneyBean = (HSRecharBalanceAdapter.MoneyBean)adapter.getItem(position);

//                moneyEdit.setFocusable(false);
//                moneyEdit.setFocusableInTouchMode(false);
//                moneyEdit.setClickable(false);
//                moneyEdit.setText("");
            }
        });

        moneyEdit.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                int leth = charSequence.length();
                if(leth >= 0){
                    adapter.setIndex(-1);
                    moneyBean = null;
                }
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        updateStatus();

    }



    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_recharge:
                if(basicInfoBean == null){
                    ToastUtil.getInstant().show(getContext(),"数据异常");
                    return;
                }

                String money = null;
                if(moneyBean != null){
                    money = moneyBean.getMoneyValue();
                }

                if(money != null && !(money.equals(""))){
                    moneyEdit.setFocusable(false);
                    moneyEdit.setFocusableInTouchMode(false);
                    moneyEdit.setClickable(false);
                }

                if(money == null || money.equals("")){
                    money = moneyEdit.getText().toString();
                }

                if(money != null && !(money.equals(""))){
                    moneyEdit.setFocusableInTouchMode(true);
                    moneyEdit.setFocusable(true);
                    moneyEdit.requestFocus();
                    moneyEdit.setClickable(true);
                }


                if(money == null || money.equals("")){
                    ToastUtil.getInstant().show(getContext(),"请选择充值余额");
                    return;
                }

                if(basicInfoBean != null){
                    try{
                        int openAccout = basicInfoBean.getIsOpenAccount();
                        if (basicInfoBean.getIsBankCardBind() != 0){                        //银行卡是否绑定 0 是绑定过，1 未绑定
                            ToastUtil.getInstant().show(getContext(),"您未绑定过银行卡，请先去绑银行卡");
                            return;
                        }
                        if(openAccout == 0){
                            Bundle bundle = new Bundle();
                            bundle.putString("money",money);
                            bundle.putString("rechargeType","3");
                            startActivity(HSReChargeActivity.class,bundle,true);
                        }else{
                            Intent intent = new Intent();
                            intent.setClass(getActivity(),HSReChargeAuthorizedActivity.class);
                            startActivity(intent);
                            getActivity().finish();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                }

                break;
            default:
                break;
        }
    }

    private FindUserBasicInfoBean basicInfoBean;
    private void updateStatus() {
        show();
        HttpParams params = new HttpParams();
        DMOKHttpUtils.getInstance().post(mContext, DMConstant.ApiURL.FIND_USER_BASIC_INFO, params, new RequestCallBack<FindUserBasicInfoBean>() {
            @Override
            public void onSuccess(JsonResult<FindUserBasicInfoBean> response) {
                dismiss();
                try{
                    if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                        basicInfoBean = response.getObject();
                        if (basicInfoBean == null){
                            basicInfoBean = new FindUserBasicInfoBean();
                        }
                    } else {
                        if (!"300001".equals(response.getCode())) {
                            if (!"300084".equals(response.getCode())) {
                                ToastUtil.getInstant().show(mContext, response.getDescription());
                            }
                        } else {
                            DMApplication.getInstance().setUserLoginInfo(null);
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(mContext, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }

        });
    }




}
